* Robustness to alternative target samples: "likely" SSI recipients
* 4/13/2025

clear all
set more off

program main
	foreach sample in max_fed120 ed_home {
		twfe_regressions, sample(`sample')
	}
	percent_effects
	make_table
	
	foreach sample in max_fed120 ed_home {
		dynamic_regressions, sample(`sample')
	}
end
	
program twfe_regressions  // use state-level treatment design
	syntax, sample(str)
	
	use "$for_analysis/state_for_regressions_`sample'", clear
	lab var inter "Treat x Post"
	local indiv_controls = "i.sex i.race i.hispan i.edcat age i.diffphys"
	local state_snap_controls = "i.has_bbce i.has_call i.has_faceini i.has_facerec i.has_oapp i.has_no_fp"
	qui su snap if relyr == -1 & !mi(startyear), d
	global base_`sample' = `r(mean)'
	
	reghdfe snap inter [w = perwt], a(i.statefip i.year) vce(cl statefip)	// no controls
	est sto nocont_`sample'
	qui estadd local cont " ", replace
	qui estadd local unemp " ", replace
	qui estadd local hhtype " ", replace
	qui estadd local stateFE "\checkmark", replace
	qui estadd local yearFE "\checkmark", replace
	global beta_nocont_`sample' = _b[inter] / ${base_`sample'}

	reghdfe snap inter `indiv_controls' [w = perwt], ///
		a(i.statefip i.year) vce(cl statefip)	// controls
	est sto cont_`sample'
	qui estadd local cont "\checkmark", replace
	qui estadd local unemp " ", replace
	qui estadd local hhtype " ", replace
	qui estadd local stateFE "\checkmark", replace
	qui estadd local yearFE "\checkmark", replace
	global beta_cont_`sample' = _b[inter] / ${base_`sample'}

	reghdfe snap inter `indiv_controls' ///
		state_unemp `state_snap_controls' ///
		[w = perwt], a(i.statefip i.year) vce(cl statefip) // add state controls
	est sto statecont_`sample'
	qui estadd local cont "\checkmark", replace
	qui estadd local unemp "\checkmark", replace
	qui estadd local hhtype " ", replace
	qui estadd local stateFE "\checkmark", replace
	qui estadd local yearFE "\checkmark", replace
	global beta_statecont_`sample' = _b[inter] / ${base_`sample'}
end

program percent_effects
	di "baseline SNAP takeup rates:"
	di "max_fed120: $base_max_fed120"
	di "ed_home: $base_ed_home"

	di "percent effects:"
	* twfe
	matrix percent_twfe = ${beta_nocont_max_fed120}, ${beta_cont_max_fed120}, ///
		${beta_statecont_max_fed120}, ${beta_nocont_ed_home}, ${beta_cont_ed_home}, ///
		${beta_statecont_ed_home}
	mat li percent_twfe
end

program make_table
	esttab nocont_max_fed120 cont_max_fed120 statecont_max_fed120 nocont_ed_home cont_ed_home ///
		statecont_ed_home, ///
		keep(inter) se(%5.3f) star(* 0.10 ** 0.05 *** 0.01) stats(yearFE stateFE cont unemp N, ///
		labels("Year FE" "State FE" "Individual Controls" "State Controls" "N" ) ///
		fmt("%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc")) scalars() ///
		nomtitles label mgroups("120\% Sample" "$\leq$ HS Ed. \& Non-homeowner", pattern(1 1) ///
		prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		nonote booktabs

	esttab nocont_max_fed120 cont_max_fed120 statecont_max_fed120 nocont_ed_home cont_ed_home ///
		statecont_ed_home using ///
		"$output/robustness_twfe.tex", replace ///
		keep(inter) se(%5.3f) star(* 0.10 ** 0.05 *** 0.01) stats(yearFE stateFE cont unemp N, ///
		labels("Year FE" "State FE" "Individual Controls" "State Controls" "N" ) ///
		fmt("%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc" "%9.0fc")) scalars() ///
		nomtitles label mgroups("120\% Sample" "$\leq$ HS Ed. \& Non-homeowner", pattern(1 1) ///
		prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		nonote booktabs
end

program dynamic_regressions
	syntax, sample(str)
	
	use "$for_analysis/state_for_regressions_`sample'", clear
	local indiv_controls = "i.sex i.race i.hispan i.edcat age i.diffphys"
	local state_snap_controls = "i.has_bbce i.has_call i.has_faceini i.has_facerec i.has_oapp i.has_no_fp"
	local omit = 6
	
	reghdfe snap ib`omit'.relyr_pos `indiv_controls' ///
		[w = perwt], a(i.statefip i.year) vce(cl statefip)
	est sto twfe
	
	coefplot, keep(*.relyr_pos) drop(1.relyr_pos 13.relyr_pos) ///
		vertical xscale(titlegap(2)) mfcolor(white) ///
		msymbol(T) xline(6.5, lp(dash) lcolor(gs12)) ///
		yline(0, lwidth(thin) lp(dash) lcolor(black)) ylab(#7, angle(0)) graphregion(fcolor(white) ///
		lcolor(white) lwidth(vvvthin)) ciopts(recast(rcap) lcolor(red)) mcolor(red) ///
		xtitle("Years Since CAP Introduced") ytitle("Estimate and 95% CI") baselevel
	graph export "$output/robustness_`sample'_es.png", replace
end
	
	
* Execute
main
